#pragma once
#include<functional>
#include"Buffer.h"
#include"agreement.h"
#include<queue>
#include<mutex>
#include<condition_variable>

class MyChannel;
class Socket;
class EventLoop;

class TcpConnection
{
private:
    /* data */
    MyChannel *clntCh;
    Socket *clntSock;
    EventLoop *loop;

    // 自定义的缓冲区 用于读写
    Buffer readBuff;
    Buffer writeBuff;

    bool sending_ = false;
    int i = 0;

public:
    std::mutex sendMutex;
        //数据包
    std::queue<CloudDrive::Packet> sendQueue;

    std::condition_variable sendcv;

public:
    TcpConnection(/* args */);
    TcpConnection(EventLoop * _loop,Socket *_clntSock);

    ~TcpConnection();
    //读事件
    void readEcho(int sockfd);
    //写事件
    void writeEcho(int sockfd);

    void setDeleteConnectionCallback(std::function<void(Socket*)> _cb);
    void setDataHandleCallback(std::function<void(Buffer&)>_cb);


    std::function<void(Socket*)> deleteConnectionCallback;
    std::function<void(Buffer &)> dataHandlerCallback;
    // std::function<void(const CloudDrive::Packet &packet)> dataWriteCallback;

    void sendData(const CloudDrive::Packet &packet);

    void enqueueSend(const CloudDrive::Packet &packet);

    int getFd();
};


